﻿package effects
{	
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	import classes.TweenLite;
	import classes.Easing;
	
	public class Effect1 extends Effect
	{	
		private var url:String = "assets/jpeg1.jpg";
		private var loader:Loader = new Loader();
		private var matrix:Matrix;
		private var myBitmap:BitmapData;
		private var w:int;
	 	private var h:int;
	 	private var mat0:Array;
	 	private var mat1:Array;
	 	private var cmat:Array;
	 	private var colorMat:ColorMatrixFilter; 
		private var c:Number;

		public function Effect1() 
		{
			cmat=[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0];
			mat0=[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0];
		 	mat1=[-0.574,1.43,0.144,0,0,0.426,0.43,0.144,0,0,0.426,1.43,-0.856,0,0,0,0,0,1,0];
			
			c=1;
			cacheAsBitmap=true;
			var request:URLRequest = new URLRequest(url);
            
	    loader.load(request);
	    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, draw);
	    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		public override function add():void
		{
			addEventListener(Event.ENTER_FRAME,render);
		}
		public override function remove():void
		{
			removeEventListener(Event.ENTER_FRAME,render);
			c=Math.random()*10;
			matrix.invert();
			matrix.rotate(Math.PI/c); 
		}
		private function draw(event:Event):void 
		{
			myBitmap = new BitmapData(loader.width, loader.height, false);
			myBitmap.draw(loader, new Matrix());
			matrix = new Matrix();
			matrix.invert();
			matrix.rotate(Math.PI/c);   
			with(graphics){
				beginBitmapFill(myBitmap, matrix, true,true);
				drawRect(0, 0, Global._stage.stageWidth, Global._stage.stageHeight);
				endFill();
			}
			forward();
			addEventListener(Event.ENTER_FRAME,render);
		}
		private function render(e:Event):void
		{
			with(graphics){
	    	 	clear();
			    beginBitmapFill(myBitmap, matrix, true,true);
			    drawRect(0, 0, Global._stage.stageWidth, Global._stage.stageHeight);
			    endFill();
			}
			var colorMat:ColorMatrixFilter = new ColorMatrixFilter(cmat);
			filters = [colorMat];
		}
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			ExternalInterface.call("alert","Unable to load image: " + url);
		}
		private function forward():void
		{
			TweenLite.to(cmat, Math.random()*2+Math.random()*2, {endArray:[-Math.random(),Math.random()+.5,Math.random()*.5,Math.random()*.5,0,Math.random(),Math.random(),Math.random(),0,-Math.random()*.5,Math.random(),Math.random()+.5,-Math.random(),0,0,0,0,0,1,0],onComplete:reverse,ease:Easing.EXPO_EASE_IN});
		}
		private function reverse():void
		{
			TweenLite.to(cmat, Math.random()*2+Math.random()*2, {endArray:[Math.random()*Math.random()*-1,Math.random()+.5,Math.random()*.5,0,Math.random()*.5,Math.random(),Math.random(),Math.random(),-Math.random()*.5,0,Math.random(),Math.random()+.5,-Math.random(),0,0,0,0,0,1,0],onComplete:forward,ease:Easing.EXPO_EASE_OUT});	
		}
	}
}